package com.hunter.jerrymouse.request.netty;

import com.hunter.jerrymouse.request.Request;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.http.*;

import java.util.Map;

/**
 * Created by hunter
 * <p>
 * 代理Netty请求的 请求实现类, 同时添加一些自己的参数
 */
public class NettyRequest implements FullHttpRequest, Request {

    /**
     * 接收时间
     */
    private long receivedTime;

    /**
     * 处理过
     */
    private boolean handled;

    /**
     * 被代理的请求体
     */
    private FullHttpRequest httpRequest;

    /**
     * 请求体数据
     */
    private ByteBuf requestData;

    /**
     * 参数列表
     */
    private Map<String, Object> paramMap;

    public NettyRequest() {
    }

    public NettyRequest(FullHttpRequest httpRequest) {
        this.httpRequest = httpRequest;
    }

    public long getReceivedTime() {
        return receivedTime;
    }

    public void setReceivedTime(long receivedTime) {
        this.receivedTime = receivedTime;
    }

    public boolean getHandled() {
        return handled;
    }

    public void setHandled(boolean handled) {
        this.handled = handled;
    }

    public ByteBuf getRequestData() {
        return requestData;
    }

    public void setRequestData(ByteBuf requestData) {
        this.requestData = requestData;
    }

    public Map<String, Object> getParamMap() {
        return paramMap;
    }

    public void setParamMap(Map<String, Object> paramMap) {
        this.paramMap = paramMap;
    }

    @Override
    public String getRealUri() {
        if (this.getUri() == null) {
            return null;
        }

        return this.getUri().split("\\?")[0];
    }

    @Override
    public String getHeader(String headerName) {
        return this.headers().get(headerName);
    }

    @Override
    public String getHttpMethod() {
        return this.getMethod().name();
    }

    @Override
    public int getContentLength() {
        return HttpUtil.getContentLength(this.httpRequest, 0);
    }

    @Override
    public String getContentType() {
        return this.headers().get(HttpHeaderNames.CONTENT_TYPE);
    }

    @Override
    public Object getParameter(String paramName) {
        if (this.paramMap == null) {
            return null;
        }

        return paramMap.get(paramName);
    }

    @Override
    public boolean keepAlive() {
        return HttpUtil.isKeepAlive(httpRequest);
    }

    //~~-------------- 以下方法仅为代理FullHttpRequest

    @Override
    public String uri() {
        return httpRequest.uri();
    }

    @Override
    public FullHttpRequest setUri(String s) {
        return httpRequest.setUri(s);
    }

    @Override
    public HttpVersion getProtocolVersion() {
        return httpRequest.getProtocolVersion();
    }

    @Override
    public HttpVersion protocolVersion() {
        return httpRequest.protocolVersion();
    }

    @Override
    public HttpHeaders trailingHeaders() {
        return httpRequest.trailingHeaders();
    }

    @Override
    public ByteBuf content() {
        return httpRequest.content();
    }

    @Override
    public FullHttpRequest copy() {
        return httpRequest.copy();
    }

    @Override
    public FullHttpRequest duplicate() {
        return httpRequest.duplicate();
    }

    @Override
    public FullHttpRequest retainedDuplicate() {
        return httpRequest.retainedDuplicate();
    }

    @Override
    public FullHttpRequest replace(ByteBuf byteBuf) {
        return httpRequest.replace(byteBuf);
    }

    @Override
    public FullHttpRequest retain(int i) {
        return httpRequest.retain(i);
    }

    @Override
    public int refCnt() {
        return httpRequest.refCnt();
    }

    @Override
    public FullHttpRequest retain() {
        return httpRequest.retain();
    }

    @Override
    public FullHttpRequest touch() {
        return httpRequest.touch();
    }

    @Override
    public FullHttpRequest touch(Object o) {
        return httpRequest.touch(o);
    }

    @Override
    public boolean release() {
        return httpRequest.release();
    }

    @Override
    public boolean release(int i) {
        return httpRequest.release(i);
    }

    @Override
    public FullHttpRequest setProtocolVersion(HttpVersion httpVersion) {
        return httpRequest.setProtocolVersion(httpVersion);
    }

    @Override
    public HttpHeaders headers() {
        return httpRequest.headers();
    }

    @Override
    public HttpMethod getMethod() {
        return httpRequest.getMethod();
    }

    @Override
    public HttpMethod method() {
        return httpRequest.method();
    }

    @Override
    public FullHttpRequest setMethod(HttpMethod method) {
        return httpRequest.setMethod(method);
    }

    @Override
    public String getUri() {
        return httpRequest.uri();
    }

    @Override
    public DecoderResult getDecoderResult() {
        return httpRequest.getDecoderResult();
    }

    @Override
    public DecoderResult decoderResult() {
        return httpRequest.decoderResult();
    }

    @Override
    public void setDecoderResult(DecoderResult decoderResult) {
        httpRequest.setDecoderResult(decoderResult);
    }
}
